<?
/*
%%%copyright%%%
 * phpMyTicket - ticket reservation system
 * Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of phpMyTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 * Licencees holding a valid "phpmyticket professional licence" version 1
 * may use this file in accordance with the "phpmyticket professional licence"
 * version 1 Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * The "phpmyticket professional licence" version 1 is available at
 * http://www.phpmyticket.com/ and in the file
 * PROFESSIONAL_LICENCE included in the packaging of this file.
 * For pricing of this licence please contact us via e-mail to 
 * info@phpmyticket.com.
 * Further contact information is available at http://www.phpmyticket.com/
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@phpmyticket.com if any conditions of this licencing isn't 
 * clear to you.
 
 */
?>
<?
require_once("admin/AdminView.php");
require_once("classes/ShopDB.php");

class DiscountView extends AdminView{




function discount_view (&$data){
  echo "<table  class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='2'>{$data['discount_name']}</td></tr>";   
     
  $this->print_field('discount_id',$data );
  $this->print_field('event_name',$data );
  $this->print_field('discount_name',$data );
  $this->print_field('discount_type',$data );
  $this->print_field('discount_value',$data );
  echo "</table>\n";
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?discount_event_id={$data['discount_event_id']}'>".admin_list."</a></center>";

}



function discount_form (&$data, &$err,$title){
  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>\n";
  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='2'>".$title."</td></tr>"; 
    
  $this->print_field('discount_id',$data );
  $this->print_field('event_name',$data);

  $this->print_input('discount_name',$data,$err,30,50);
  $this->print_select ("discount_type",$data,$err,array("fixe","percent"));
  
  $this->print_input('discount_value',$data,$err,6,5);
  echo "<tr><td align='center' class='admin_value' colspan='2'>
   <input type='submit' name='submit' value='".save."'>
  <input type='reset' name='reset' value='".res."'></td></tr>";

  echo "</table>\n";

  echo "<input type='hidden' name='discount_event_id' value='{$data['discount_event_id']}'/>\n";
  echo "<input type='hidden' name='event_name' value='".htmlentities($data['event_name'],ENT_QUOTES)."'/>\n";

  if($data['discount_id']){
    echo "<input type='hidden' name='discount_id' value='{$data['discount_id']}'/>\n";
    echo "<input type='hidden' name='action' value='update'/>\n";
  }else{
    echo "<input type='hidden' name='action' value='insert'/>\n";
  }
  

  echo "</form><br><center>
  <a class='link' href='{$_SERVER['PHP_SELF']}?discount_event_id={$data['discount_event_id']}'>".admin_list."</a></center>";
}

function discount_check (&$data, &$err){

  if(empty($data['discount_name'])){
    $err['discount_name']=mandatory;
  }
    
  if(empty($data['discount_value'])){$err['discount_value']=mandatory;} 

  return empty($err);
}

function discount_list ($discount_event_id){
  global $_SHOP;
  $query="SELECT event_name,ort_name,event_status FROM Event,Ort WHERE event_id='$discount_event_id' and ort_id=event_ort_id";
  if(!$names=ShopDB::query_one_row($query)){
    user_error(mysql_error());
    return;
  }


  $query="SELECT * FROM Discount WHERE discount_event_id='$discount_event_id'";
  if(!$res=ShopDB::query($query)){
    user_error(mysql_error());
    return;
  }
  
  $alt=0;
  echo "<table class='admin_list' width='500' cellspacing='0' cellpadding='4'>\n";
  echo "<tr><td  colspan='6' align='center'><div class='admin_list_title'>{$names['event_name']}</div>";
  echo "<div><i>{$names['ort_name']}</i></div></td></tr>\n";
  echo "<tr><td class='admin_list_subtitle' colspan='6'  align='center'>".discount_title."</td></tr>\n";

  while($row=mysql_fetch_assoc($res)){
    echo "<tr class='admin_list_row_$alt'>";
    echo "<td class='admin_list_item'>{$row['discount_id']}</td>\n";
    echo "<td class='admin_list_item'>{$row['discount_name']}</td>\n";
    if($row['discount_type']=='percent'){
      $type="%";
    }else if($row['discount_type']=='fixe'){ 
      $type=$_SHOP->currency;
    }
    echo "<td class='admin_list_item'>{$row['discount_value']}$type</td>\n";
    echo "<td class='admin_list_item' width='20'><a class='link' href='{$_SERVER['PHP_SELF']}?action=view&discount_id={$row['discount_id']}&discount_event_id=$discount_event_id'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a></td>\n";
    echo "<td class='admin_list_item' width='20'><a class='link' href='{$_SERVER['PHP_SELF']}?action=edit&discount_id={$row['discount_id']}&discount_event_id=$discount_event_id'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a></td>\n";

    if($names['event_status']=='unpub' or $names['event_status']=='nosal'){
      echo "<td class='admin_list_item' width='20'><a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"{$_SERVER['PHP_SELF']}?action=remove&discount_id={$row['discount_id']}&discount_event_id=$discount_event_id\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a></td>\n";
    }else{
      echo "<td></td>";
    }
    echo "</tr>";
    $alt=($alt+1)%2;
  }
  echo "</table>\n";
  
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=add&discount_event_id=$discount_event_id'>".add."</a></center>";
}


function draw (){

if($_POST['action']=='insert'){
  if(!$this->discount_check($_POST,$err)){
    $this->discount_form($_POST,$err,discount_add_title);
  }else{
    $query="INSERT Discount (
    discount_event_id,
    discount_name,
    discount_type,
    discount_value
    ) VALUES (
    '".$this->q($_POST['discount_event_id'])."',
    '".$this->q($_POST['discount_name'])."', 
    '".$this->q($_POST['discount_type'])."',
    '".$this->q($_POST['discount_value'])."'
    )";
    if(!ShopDB::query($query)){
      user_error(mysql_error());
      return;
    }

    $this->discount_list($_POST['discount_event_id']);
 }
}else

if($_POST['action']=='update'){
  if(!$this->discount_check($_POST,$err)){
    $this->discount_form($_POST,$err,discount_update_title);
  }else{

    $query="UPDATE Discount SET 
    discount_event_id='".$this->q($_POST['discount_event_id'])."',
    discount_name='".$this->q($_POST['discount_name'])."',
    discount_type='".$this->q($_POST['discount_type'])."',
    discount_value='".$this->q($_POST['discount_value'])."'
  
    WHERE 
    discount_id='".$this->q($_POST['discount_id'])."'";

    if(!ShopDB::query($query)){
      user_error(mysql_error());
      return;
    }
    $this->discount_list($_POST['discount_event_id']);
  }
}else
if($_GET['action']=='add'){
  $query="SELECT * FROM  Event WHERE event_id='{$_GET['discount_event_id']}'";
  if(!$row=ShopDB::query_one_row($query)){
    user_error(mysql_error());
    return;
  }
  $row['discount_event_id']=$row['event_id'];
  $this->discount_form($row,$err,discount_add_title);
}else 
if($_GET['action']=='edit'){
  $query="SELECT * FROM Discount, Event WHERE discount_id='{$_GET['discount_id']}' and discount_event_id=event_id";
  if(!$row=ShopDB::query_one_row($query)){
    return;
  }
  $this->discount_form($row,$err,discount_update_title);
}else 
if($_GET['action']=='view'){
  $query="SELECT * FROM Discount,Event WHERE discount_id='{$_GET['discount_id']}' and discount_event_id=event_id";
  if(!$row=ShopDB::query_one_row($query)){
    return;
  }
  $this->discount_view($row);
}else 
if($_GET['action']=='remove' and $_GET['discount_id']>0){
	$discount_id=(int)$_GET['discount_id'];
	$query="SELECT count(*) as count from Seat where seat_discount_id='$discount_id'";
	if(!$count=ShopDB::query_one_row($query)){
	  return;
	}

	if($count[0]!=0){
	  echo "<div class=error>".in_use."</div>";
		return;
	}
	
  $query="DELETE FROM Discount WHERE discount_id='{$_GET['discount_id']}' LIMIT 1";
  if(!ShopDB::query($query)){
    return;
  }
  $this->discount_list($_GET['discount_event_id']);
}else{
  $this->discount_list($_GET['discount_event_id']);
}
}
}
?>
